"""contribution model"""
from django.db import models
from .base import TimeModel
from .organization import User
from .space import Repository

class RepositoryStatus(TimeModel):
    repo = models.ForeignKey(
        Repository, 
        on_delete=models.CASCADE, 
        related_name='repository_statuses', 
        related_query_name='repository_status', 
        help_text='代码仓',
        )
    watch_change_num = models.IntegerField(help_text='watch_change数量')
    star_change_num = models.IntegerField(help_text='star_change数量')
    fork_change_num = models.IntegerField(help_text='fork_change数量')
    scan_date = models.DateField(help_text='日期')

    def __str__(self):
        return f'{self.repo.name}-{self.scan_date}'

    class Meta:
        db_table = 'contribution_repositorystatus'
        unique_together = ('repo', 'scan_date')


class Issue(TimeModel):
    
    issue_status_map={
        'open': 0,
        'close': 1
    }


    unique_tag = models.IntegerField(unique=True, help_text='issue唯一标识')
    repo = models.ForeignKey(
        Repository, 
        on_delete=models.CASCADE, 
        related_name='issues', 
        related_query_name='issue', 
        help_text='代码仓',
        )
    title = models.CharField(max_length=256, help_text='标题')
    desc = models.TextField(help_text='描述')
    author = models.ForeignKey(
        User, 
        on_delete=models.PROTECT, 
        related_name='issues', 
        related_query_name='issue', 
        help_text='作者',
        )
    status = models.IntegerField(help_text='状态')  # todo枚举值跟随gitcode
    priority = models.IntegerField(help_text='优先级')  # todo枚举值跟随gitcode
    delay_duration = models.IntegerField(help_text='停留时长（s）')
    first_resp_duration = models.IntegerField(help_text='首次响应时长（s）')
    delayed_reply_duration = models.IntegerField(help_text='超期回复时长（s）')
    first_resp_time = models.DateTimeField(null=True, help_text='首次响应时间')
    last_resp_time = models.DateTimeField(null=True, help_text='最新响应时间')
    close_loop_time = models.DateTimeField(null=True, help_text='闭环时间')
    updated = models.DateTimeField(help_text='gitcode更新时间')
    created = models.DateTimeField(help_text='gitcode创建时间')

    def __str__(self):
        return f'{self.repo.name}-{self.updated}'

    class Meta:
        db_table = 'contribution_issue'


class PullRequest(TimeModel):
    unique_tag = models.IntegerField(unique=True, help_text='pr唯一标识')
    repo = models.ForeignKey(
        Repository, 
        on_delete=models.CASCADE, 
        related_name='pull_requests', 
        related_query_name='pull_request', 
        help_text='代码仓',
        )
    title = models.CharField(max_length=256, help_text='标题')
    desc= models.TextField(help_text='描述')
    author = models.ForeignKey(
        User,
        on_delete=models.PROTECT, 
        related_name='pull_requests', 
        related_query_name='pull_request', 
        help_text='作者',
        )
    branch = models.CharField(max_length=256, help_text='分支')
    updated = models.DateTimeField(help_text='gitcode更新时间')
    created = models.DateTimeField(help_text='gitcode创建时间')

    def __str__(self):
        return f'{self.repo.name}-{self.updated}'

    class Meta:
        db_table = 'contribution_pullrequest'
    

class Advice(TimeModel):
    unique_tag = models.IntegerField(unique=True, help_text='advice唯一标识')
    pull_request = models.ForeignKey(
        PullRequest, 
        on_delete=models.CASCADE, 
        related_name='advices', 
        related_query_name='advice', 
        help_text='所属Pr',
        )
    author = models.ForeignKey(
        User, 
        on_delete=models.PROTECT, 
        related_name='advices', 
        related_query_name='advice', 
        help_text='作者',
        )
    comment = models.TextField(help_text='内容')
    updated = models.DateTimeField(help_text='gitcode更新时间')
    created = models.DateTimeField(help_text='gitcode创建时间')

    def __str__(self):
        return f'{self.author.username}-{self.update_time}'

    class Meta:
        db_table = 'contribution_advice'